package com.park.map;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;

import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.style.layers.FillExtrusionLayer;
import com.mapbox.mapboxsdk.style.layers.FillLayer;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MapActivity extends AppCompatActivity {

    private MapView mapView;
    private MapboxMap map;
    private Style mapStyle;
    public final double[] BOUNDS = new double[]{34.706284194, 113.763065513, 34.713145089, 113.768609914};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置Mapbox的key
        Mapbox.getInstance(this, this.getResources().getString(R.string.mapbox_access_token));
        setContentView(R.layout.activity_map);

        mapView = findViewById(R.id.mapView);


        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull MapboxMap mapboxMap) {
                map = mapboxMap;
                //创建空白底图
                String style="{\"version\":8,\"name\":\"Dark\",\"sources\":{\"mapbox\":{\"type\":\"vector\",\"url\":\"mapbox://mapbox.mapbox-streets-v8\"}},\"sprite\":\"mapbox://sprites/mapbox/dark-v10\",\"glyphs\":\"mapbox://fonts/mapbox/{fontstack}/{range}.pbf\",\"layers\":[]}";
                Style.Builder builder=new Style.Builder().fromJson(style);
                mapboxMap.setStyle(builder, new Style.OnStyleLoaded() {


                    @Override
                    public void onStyleLoaded(@NonNull Style style) {
                        mapStyle=style;
                        //底图缩放至停车场位置
                        CameraPosition position = new CameraPosition.Builder()
                                .target(new LatLng((BOUNDS[0]+BOUNDS[2])/2, (BOUNDS[1]+BOUNDS[3])/2))
                                .zoom(15)
                                .tilt(30)
//                                .bearing(20)
                                .build();

//                        LatLngBounds latLngBounds = LatLngBounds.from(BOUNDS[2], BOUNDS[3], BOUNDS[0], BOUNDS[1]);
//                        CameraPosition position = map.getCameraForLatLngBounds(latLngBounds);
//                        CameraPosition position1=position.bearing(position);
                        map.setCameraPosition(position);
                        addJson();
                    }
                });
            }
        });
    }
    private void addJson(){
        //以geoJSon的方式添加分层数据源
        readJson("ground");
        readJson("runway");
        readJson("stone");
        readJson("wall");
        readJson("park");
        readJson("other");
        //添加图层，并设置对应的颜色
        addFillLayer("ground","#00BFFF");
        addFillLayer("runway","#7C8BB4");
        addFillLayer("park","#00FF00");
        addFillLayer("other","#C5F9FA");
        //添加3D图层
        addFill3DLayer("wall","#007FFF",30f);
        addFill3DLayer("stone","#C0C0C0",210f);

    }

    public void readJson(String name) {

        try {
            //读取GeoJSon数据文件
            AssetManager assetManager = getAssets(); // 获得assets资源管理器(assets中的文件无法直接访问，可以使用AssetManager访问)

            InputStreamReader inputStreamReader = null; // 使用IO流读取json文件内容

            inputStreamReader = new InputStreamReader(assetManager.open(name+".json"), "UTF-8");


            BufferedReader br = new BufferedReader(inputStreamReader);

            String line;

            StringBuilder builder = new StringBuilder();

            while ((line = br.readLine()) != null) {

                builder.append(line);

            }
            br.close();//关闭文件流
            inputStreamReader.close();
            Log.e("TAG",name);
            //添加数据源到地图
            GeoJsonSource source = new GeoJsonSource(name);
            source.setGeoJson(builder.toString());
            mapStyle.addSource(source);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加图层
     * @param name
     * @param color
     */
    public void addFillLayer(String name,String color){
        FillLayer fillLayer=new FillLayer(name,name).withProperties(PropertyFactory.fillColor(color));

        mapStyle.addLayer(fillLayer);
    }

    /**
     * 添加3D图层
     * @param name
     * @param color
     * @param height
     */
    public void addFill3DLayer(String name,String color,float height){
        FillExtrusionLayer fillLayer=new FillExtrusionLayer(name,name).withProperties(
                PropertyFactory.fillExtrusionColor(color)
        ).withProperties(PropertyFactory.fillExtrusionHeight(height));

        mapStyle.addLayer(fillLayer);
    }
}